package com.wyl.service;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wyl.mapper.SimpleMapper;
import com.wyl.model.Simple;

@Service("simpleService")
public class SimpleService {

	@Autowired
	SimpleMapper simpleMapper;
	
	public void save(Simple simple){
		Simple temp = simpleMapper.selectByDocurl(simple.getDocurl());
		String dyPattern = "http://dy.*";
		Pattern dyr = Pattern.compile(dyPattern);
		Matcher dym = dyr.matcher(simple.getDocurl());
		if(dym.find()||temp!=null) return ;
			
		simpleMapper.save(simple.getTitle(), simple.getDocurl(), simple.getTime(), simple.getChannelname(),
				simple.getImgurl(), simple.isHasContent());
	}
	
	public List<Simple> selectByTypeAndPage(String type,int page,int limit){
		int offset = (page-1)*limit;
		return simpleMapper.selectByOffsetAndType(type, offset, limit);
	}
	
	public List<Simple> selectNoContent() {
		return simpleMapper.selectNoContent();
	}
	
	public void clearTable(){
		simpleMapper.clear();
	}
	
	public Simple selectByDocurl(String url){
		return simpleMapper.selectByDocurl(url);
	}

	public void setContentState(Integer id,boolean hasContent) {
		simpleMapper.updateContentState(id, hasContent);
		
		
	}

	public List<Simple> selectAll() {
		
		return simpleMapper.selectAll();
	}
}
